import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CacheModule } from '@nestjs/cache-manager';
import { ThrottlerModule } from '@nestjs/throttler';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { RoomsController } from './rooms.controller';
import { RoomsService } from './rooms.service';
import { Room } from './entities/room.entity';
import { Participant } from './entities/participant.entity';
import { RedisModule } from '../redis/redis.module';

@Module({
  imports: [
    TypeOrmModule.forFeature([Room, Participant]),
    RedisModule,
    ConfigModule,
    CacheModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        ttl: configService.get('app.cache.ttl', 300), // 5分钟缓存
        max: configService.get('app.cache.max', 100), // 最大缓存项数
      }),
      inject: [ConfigService],
    }),
    ThrottlerModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => [
        {
          name: 'short',
          ttl: configService.get('app.throttle.shortTtl', 1000), // 1秒
          limit: configService.get('app.throttle.shortLimit', 3), // 3次请求
        },
        {
          name: 'medium',
          ttl: configService.get('app.throttle.mediumTtl', 10000), // 10秒
          limit: configService.get('app.throttle.mediumLimit', 20), // 20次请求
        },
        {
          name: 'long',
          ttl: configService.get('app.throttle.longTtl', 60000), // 1分钟
          limit: configService.get('app.throttle.longLimit', 100), // 100次请求
        },
      ],
      inject: [ConfigService],
    }),
  ],
  controllers: [RoomsController],
  providers: [RoomsService],
  exports: [RoomsService],
})
export class RoomsModule {}
